package com.incar.chuxing.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.incar.chuxing.R;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.bean.CountDownCloseEvent;
import com.incar.chuxing.bean.CouponCountDownBean;
import com.incar.chuxing.utils.DialogQueue;
import com.incar.chuxing.utils.Dialogs.CountDownDialog;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.RxBus;
import com.incar.chuxing.utils.TimeUtils;

import java.util.ArrayList;

public class CouponCountDown extends LinearLayout {
    private Context context;
    private RelativeLayout layoutCountDown;
    private TextView countDownTime;
    private TextView countdownText;
    private TextView countdownDone;
    private ImageView countdownClose;
    private TextView countdownHow;
    private int status;
    private CouponCountDownBean couponCountDownBean;

    public CouponCountDown(Context context) {
        super(context);
        init(context);
    }


    public CouponCountDown(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CouponCountDown(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_countdown, this);

        layoutCountDown = findViewById(R.id.layout_countdown);
        countDownTime = findViewById(R.id.countdown_time);
        countdownText = findViewById(R.id.countdown_text);
        countdownDone = findViewById(R.id.countdown_done);
        countdownClose = findViewById(R.id.countdown_close);
        countdownHow = findViewById(R.id.countdown_how);

    }

    public void initTime(Context context, CouponCountDownBean couponCountDownBean) {
        this.couponCountDownBean = couponCountDownBean;
        setVisibility(View.VISIBLE);
        countdownText.setText(couponCountDownBean.getAttachment().getTextMessage());
        countdownHow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹框
                popPost(context, couponCountDownBean);
            }
        });
    }

    public void update(long remain) {
        layoutCountDown.setBackgroundResource(R.drawable.countdown_bg);
        countdownClose.setVisibility(View.GONE);
        countdownDone.setVisibility(View.GONE);
        countdownHow.setVisibility(View.VISIBLE);
        setVisibility(View.VISIBLE);
        countDownTime.setText(TimeUtils.formatSecondCount(remain + "", 2));
        status = 1;//显示
    }


    public void stop() {
        setVisibility(View.VISIBLE);
        layoutCountDown.setBackgroundResource(R.drawable.countdown_invalid_bg);
        countDownTime.setText("00:00");
        countdownClose.setVisibility(View.VISIBLE);
        countdownDone.setVisibility(View.VISIBLE);
        countdownHow.setVisibility(View.GONE);
        countdownClose.setOnClickListener(v -> {
            setVisibility(View.GONE);
            status = 2;//隐藏
            RxBus.getInstance().post(new CountDownCloseEvent(true));
        });
        status = 0;//过期
    }


    public int getStatus() {
        return status;
    }

    public void setVisible(int visible) {
        setVisibility(visible);
        status = 2;

    }

    public CouponCountDownBean getData() {
        return couponCountDownBean;
    }

    public void popPost(Context context, CouponCountDownBean couponCountDownBean) {
        Logger.d("couponDown", "step5");
        ArrayList<String> imageList = new ArrayList<>();
        ArrayList<String> urlList = new ArrayList<>();
        ArrayList<String> titleList = new ArrayList<>();
        imageList.add(couponCountDownBean.getAttachment().getImg_link());
        urlList.add(couponCountDownBean.getAttachment().getDesHtmlLink());
        titleList.add(" ");
        Glide.with(MyApplication.getInstance()).load(imageList.get(0)).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                CountDownDialog postDialog = new CountDownDialog(context, imageList, urlList, titleList);
                DialogQueue.getInstance().showDialog(postDialog);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
            }
        });
    }


}
